package com.lujianfei.androidroom;

import android.content.Context;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.lujianfei.androidroom.database.AppDatabase;
import com.lujianfei.androidroom.database.UserEntity;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    final static String TAG = "ExampleInstrumentedTest==";

    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.lujianfei.androidroom", appContext.getPackageName());
    }

    @Test
    public void insertTest() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        UserEntity userEntity = new UserEntity();
        userEntity.userName = "小明";
        userEntity.updateTime = System.currentTimeMillis();
        userEntity.createTime = userEntity.updateTime;
        long[] longs = AppDatabase.getInstance(appContext).userDao().insertUser(userEntity);
        log("insertUser " + userEntity + " return id = " + longs[0]);
    }

    @Test
    public void queryAllTest() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        List<UserEntity> userEntities = AppDatabase.getInstance(appContext).userDao().queryAll();
        log("queryAll " + userEntities);
    }


    @Test
    public void queryUserById() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        List<UserEntity> userEntities = AppDatabase.getInstance(appContext).userDao().queryUserById(1);
        log("queryUserById " + userEntities.get(0));
    }

    void log(String text) {
        Log.d(TAG,text);
    }
}
